package com.qf.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

/**
 * @author 刘亚康
 * @Date 2020/4/5
 * @Version 1.0'
 * @Description
 */
public class HttpUtils {
    public static String sendUrlUtils(String sendUrl){
        StringBuffer stringBuffer=new StringBuffer();

        try {
            URL url = new URL(sendUrl);
            //发送这个链接
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            //设置请求的方式
            urlConnection.setRequestMethod("GET");

            //读取响应的内容 设置权限
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);

            //判断读取的状态
            if (urlConnection.getResponseCode()==200) {
                //获取读取流
                InputStream inputStream = urlConnection.getInputStream();
                Scanner scanner = new Scanner(inputStream);
                while (scanner.hasNextLine()){
                    stringBuffer.append(scanner.next());
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }
}
